<?php

namespace app\index\controller;


use app\helper\Helper;
use app\index\support\SMS\JGSMS;
use think\Controller;
use think\Db;
use think\Session;
use think\Validate;

class Login extends Base
{
    protected $require_login = false;

    public function login()
    {
        return $this->fetch('login/login');
    }

    public function login_in()
    {
        $param = $this->request->param();
        $info = Db::table('users')->where([
            'delete_ts' => 0,
            'mobile' => $param['mobile'],
            'password' => md5($param['password'])
        ])->find();
        if (!empty($info)) {
            Session::set('user_info', $info);
            return Helper::success([], '登录成功');
        } else {
            return Helper::error([], '手机号或密码错误');
        }
    }

    public function register()
    {
        return $this->fetch('login/register');
    }

    public function register_in()
    {
        $param = $this->request->param();
        $rule = [
            'username|用户名' => 'require',
            'password|密码' => 'require',
            'password1|确认密码' => 'require',
            'phone|手机号' => 'require',
        ];
        $validate = new Validate($rule);
        if (!$validate->check($param)) {
            return Helper::error([], $validate->getError());
        }
        if ($param['password'] != $param['password1']) {
            return Helper::error([[], '密码不一致']);
        }
        $res = Db::table('users')->where('user_name', $param['username'])->find();
        if (!empty($res)) {
            return Helper::error([], '用户名已存在');
        }
        $res = Db::table('users')->where('mobile', $param['phone'])->find();
        if (!empty($res)) {
            return Helper::error([], '手机号已存在');
        }
        $info = Db::table('users')->insert($data = [
            'user_name' => $param['username'],
            'mobile' => $param['phone'],
            'email' => $param['email'],
            'add_time' => time(),
            'password' => md5($param['password']),
            'delete_ts' => 0
        ]);
        if (!empty($info)) {
            $user = Db::table('users')->where('id', Db::table('users')->getLastInsID())->find();
            Session::set('user_info', $user);
            return Helper::success([], '注册成功');
        } else {
            return Helper::error([], '注册失败');
        }
    }

    public function login_out()
    {
        Session::delete('user_info');
        $this->redirect('index/index');
    }


    public function forget()
    {
        return $this->fetch('login/forget');
    }

    public function forget_in()
    {
        $param = $this->request->param();
        $rule = [
            'password|原密码' => 'require',
            'password1|确认密码' => 'require',
            'password2|确认密码' => 'require',
            'phone|手机号' => 'require',
        ];
        $validate = new Validate($rule);
        if (!$validate->check($param)) {
            return Helper::error([], $validate->getError());
        }
        if ($param['password1'] != $param['password2']) {
            return Helper::error([[], '密码不一致']);
        }
        $where = [
            'mobile'=> $param['phone'],
            'password'=> md5($param['password']),
        ];
        $res = Db::table('users')->where($where)->find();
        if (empty($res)) {
            return Helper::error([], '手机号不存在');
        }
        $info = Db::table('users')->where($where)->update(['password'=>md5($param['password1'])]);
        if (!empty($info)) {
            $user = Db::table('users')->where($where)->find();
            Session::set('user_info', $user);
            return Helper::success([], '修改成功');
        } else {
            return Helper::error([], '修改失败');
        }
    }

}
